package com.ddlaw.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ddlaw.entity.TLawThemes;
import com.ddlaw.entity.TThemesComments;
import com.ddlaw.entity.TUsers;
import com.ddlaw.service.LawThemeService;
import com.ddlaw.util.XssFilter;

@Controller
@RequestMapping("/topic")
public class TopicController extends BaseController{
	
	@Resource
	LawThemeService themeService;
	
	/***
	 * 热门话题栏目
	 * @param type	话题类型
	 * @param model		模型
	 * @return
	 */
	@RequestMapping(value="/list/{type}",method=RequestMethod.GET)
	public String index(@PathVariable(value="type") Integer type,Model model){
		List<TLawThemes>  lawThemes = themeService.getThemeById(type);
		model.addAttribute("lawThemes", lawThemes);
		model.addAttribute("themeType", type);
		return "topic/index";
	}
	
	/***
	 * 热门话题详细信息
	 * @param themeId	话题ID
	 * @param model			模型
	 * @return
	 */
	@RequestMapping(value="/detail/{themeId}",method=RequestMethod.GET)
	public String detail(@PathVariable(value="themeId") Integer themeId,Model model){
		TLawThemes lawThemes = themeService.getThemeByThemeId(themeId);
		List<TThemesComments> themesComments = themeService.getConmmentById(themeId); 
		model.addAttribute("detail", lawThemes);
		model.addAttribute("comments", themesComments);
		return "topic/detail";
	}
	
	/**
	 * 热门话题评论
	 * @param comments	评论内容详细信息
	 * @return
	 */
	@RequestMapping(value="comment",method=RequestMethod.POST)
	public String comment(TThemesComments comments){
		TUsers user = (TUsers) getHttpSession().getAttribute("user");
		if(user != null || !"".equals(comments.getCommentcon())){
			comments.setUserid(user.getUserid());
			String username = user.getUsername();
			String tel = username.substring(0, 3) + "*****" +username.substring(8);
			comments.setUsername(tel);
			comments.setCommentcon(XssFilter.cleanXSS(comments.getCommentcon()));
			themeService.addConmment(comments);
		}
		return "redirect:/topic/detail/"+comments.getThemesid();
	}
}
